<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\User\Faces\Marry\Logs as Marry;
use Excel;

class ImagesController extends Controller
{
    public function index(Request $request){
        if($request->ajax()){
            $data = array();
            $data['draw'] = $request->get('draw');
            $start = $request->get('start');
            $length = $request->get('length');
            $order = $request->get('order');
            $columns = $request->get('columns');
            $search = $request->get('search');
            $data['recordsTotal'] = Marry::count();
            if (strlen($search['value']) > 0) {
                $data['recordsFiltered'] = Marry::where(function ($query) use ($search) {
                    $query->where('name', 'LIKE', '%' . $search['value'] . '%')
                        ->orWhere('email', 'like', '%' . $search['value'] . '%');
                })->count();
                $data['data'] = Marry::where(function ($query) use ($search) {
                    $query->where('name', 'LIKE', '%' . $search['value'] . '%')
                        ->orWhere('email', 'like', '%' . $search['value'] . '%');
                })
                    ->skip($start)->take($length)
                    ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
                    ->get();
            } else {
                $data['recordsFiltered'] = Marry::count();
                $data['data'] =  Marry::with(array('FromFace'=>function($query){
                    $query->with(array(
                            'attach'=>function($query){$query->select('id','path');},
                            'users'=>function($query){$query->select('id','nickname');})
                    )->select('x_uid','attach_id','id','value');
                },'ToFace'=>function($query){
                    $query->with(array(
                            'attach'=>function($query){$query->select('id','path');},
                            'users'=>function($query){$query->select('id','nickname');})
                    )->select('x_uid','attach_id','id','value');
                }))->where('status','=',0)->skip($start)->take($length)
                    ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
                    ->get();
            }
            return response()->json($data);
        }
        return view('admin.images.index');
    }

    public function export(){
       $marry_lits = Marry::with(array('FromFace'=>function($query){
            $query->with(array(
                    'attach'=>function($query){$query->select('id','path');},
                    'users'=>function($query){$query->select('id','nickname');})
            )->select('x_uid','attach_id','id','value');
        },'ToFace'=>function($query){
            $query->with(array(
                    'attach'=>function($query){$query->select('id','path');},
                    'users'=>function($query){$query->select('id','nickname');})
            )->select('x_uid','attach_id','id','value');
        }))->where('status','=',0)->orderBy('id','desc')->get();

        $cellData[] = array('用户昵称1','图片路径1','用户昵称2','图片路径2');
        foreach ($marry_lits as $row){
            $cellData[]=array(
                $row->FromFace->users->nickname,
                $row->FromFace->attach->path,
                $row->ToFace->users->nickname,
                $row->ToFace->attach->path,
            );
        }
        Excel::create('头像匹配记录',function($excel) use ($cellData){
            $excel->sheet('score', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
    }

    public function show(){

    }
}
